﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Text;

namespace SDLITS.Web
{
    public class Memory
    {
        /// <summary>
        /// BUFF清零
        /// </summary>
        /// <param name="buff"></param>
        /// <returns></returns>
        public Boolean bzero(ref Byte[] buff)
        {
            try
            {
                for (int i = 0; i < buff.Length; i++)
                {
                    buff[i] = 0;
                }
            }
            catch (Exception e)
            {
                return false;
            }
            return true;
        }


        /// <summary>
        /// 将字符串复制到缓冲区
        /// </summary>
        /// <param name="buff"></param>
        /// <param name="buffIndex"></param>
        /// <param name="s"></param>
        /// <returns></returns>
        public Boolean memcpy(ref Byte[] buff, int buffIndex, String s)
        {
            try
            {
                Encoding.ASCII.GetBytes(s, 0, s.Length, buff, buffIndex);
            }
            catch (Exception e)
            {
                return false;
            }
            return true;
        }
        /// <summary>
        /// 将字符串复制到缓冲区
        /// </summary>
        /// <param name="buff"></param>
        /// <param name="buffIndex"></param>
        /// <param name="s"></param>
        /// <param name="charCount"></param>
        /// <returns></returns>
        public Boolean memcpy(ref Byte[] buff, int buffIndex, String s, int charCount)
        {
            try
            {
                Encoding.ASCII.GetBytes(s, 0, charCount, buff, buffIndex);
            }
            catch (Exception e)
            {
                return false;
            }
            return true;
        }
        /// <summary>
        /// 将字符串复制到缓冲区
        /// </summary>
        /// <param name="buff"></param>
        /// <param name="buffIndex"></param>
        /// <param name="s"></param>
        /// <param name="charIndex"></param>
        /// <param name="charCount"></param>
        /// <returns></returns>
        public Boolean memcpy(ref Byte[] buff, int buffIndex, String s, int charIndex, int charCount)
        {
            try
            {
                Encoding.ASCII.GetBytes(s, charIndex, charCount, buff, buffIndex);
            }
            catch (Exception e)
            {
                return false;
            }
            return true;
        }
        /// <summary>
        /// 从缓冲区复制字符
        /// </summary>
        /// <param name="s"></param>
        /// <param name="buff"></param>
        /// <returns></returns>
        public Boolean memcpy(ref String s, Byte[] buff)
        {
            try
            {
                s = Encoding.ASCII.GetString(buff);
            }
            catch (Exception e)
            {
                return false;
            }
            return true;
        }

        /// <summary>
        /// 从缓冲区复制字符
        /// </summary>
        /// <param name="s"></param>
        /// <param name="buff"></param>
        /// <param name="buffIndex"></param>
        /// <returns></returns>
        public Boolean memcpy(ref String s, Byte[] buff, int buffIndex)
        {
            try
            {
                s = Encoding.ASCII.GetString(buff, buffIndex, buff.Length - buffIndex);
            }
            catch (Exception e)
            {
                return false;
            }
            return true;
        }

        /// <summary>
        /// 从缓冲区复制字符
        /// </summary>
        /// <param name="s"></param>
        /// <param name="buff"></param>
        /// <param name="buffIndex"></param>
        /// <param name="count"></param>
        /// <returns></returns>
        public Boolean memcpy(ref String s, Byte[] buff, int buffIndex, int count)
        {
            try
            {
                s = Encoding.ASCII.GetString(buff, buffIndex, count);
            }
            catch (Exception e)
            {
                return false;
            }
            return true;
        }


        /// <summary>
        /// 将BOOLEAN值复制到缓冲
        /// </summary>
        /// <param name="buff"></param>
        /// <param name="buffIndex"></param>
        /// <param name="data"></param>
        /// <returns></returns>
        public Boolean memcpy(ref Byte[] buff, int buffIndex, Boolean data)
        {
            try
            {
                if (true == data)
                {
                    buff[buffIndex] = 1;
                }
                else
                {
                    buff[buffIndex] = 0;
                }

            }
            catch (Exception e)
            {
                return false;
            }
            return true;
        }
        /// <summary>
        /// 从缓冲中复制BOOLEAN值
        /// </summary>
        /// <param name="buff"></param>
        /// <param name="buffIndex"></param>
        /// <param name="data"></param>
        /// <returns></returns>
        public Boolean memcpy(ref Boolean data, Byte[] buff, int buffIndex)
        {
            try
            {
                if (0 == buff[buffIndex])
                {
                    data = false;
                }
                else
                {
                    data = true;
                }

            }
            catch (Exception e)
            {
                return false;
            }
            return true;
        }

        public Boolean memcpy(ref Byte[] buff, int buffIndex, Int16 data)
        {
            try
            {
                byte[] cb = BitConverter.GetBytes(data);
                for (int i = 0; i < cb.Length; i++)
                    buff[buffIndex + i] = cb[i];
            }
            catch (Exception e)
            {
                return false;
            }
            return true;
        }

        public Boolean memcpy(ref Int16 data, Byte[] buff, int buffIndex)
        {
            try
            {
                data = BitConverter.ToInt16(buff, buffIndex);
            }
            catch (Exception e)
            {
                return false;
            }
            return true;
        }

        public Boolean memcpy(ref Byte[] buff, int buffIndex, UInt16 data)
        {
            try
            {
                byte[] cb = BitConverter.GetBytes(data);
                for (int i = 0; i < cb.Length; i++)
                    buff[buffIndex + i] = cb[i];
            }
            catch (Exception e)
            {
                return false;
            }
            return true;
        }

        public Boolean memcpy(ref UInt16 data, Byte[] buff, int buffIndex)
        {
            try
            {
                data = BitConverter.ToUInt16(buff, buffIndex);
            }
            catch (Exception e)
            {
                return false;
            }
            return true;
        }

        public Boolean memcpy(ref Byte[] buff, int buffIndex, Int32 data)
        {
            try
            {
                byte[] cb = BitConverter.GetBytes(data);
                for (int i = 0; i < cb.Length; i++)
                    buff[buffIndex + i] = cb[i];
            }
            catch (Exception e)
            {
                return false;
            }
            return true;
        }

        public Boolean memcpy(ref Int32 data, Byte[] buff, int buffIndex)
        {
            try
            {
                data = BitConverter.ToInt32(buff, buffIndex);
            }
            catch (Exception e)
            {
                return false;
            }
            return true;
        }

        public Boolean memcpy(ref Byte[] buff, int buffIndex, UInt32 data)
        {
            try
            {
                byte[] cb = BitConverter.GetBytes(data);
                for (int i = 0; i < cb.Length; i++)
                    buff[buffIndex + i] = cb[i];
            }
            catch (Exception e)
            {
                return false;
            }
            return true;
        }

        public Boolean memcpy(ref UInt32 data, Byte[] buff, int buffIndex)
        {
            try
            {
                data = BitConverter.ToUInt32(buff, buffIndex);
            }
            catch (Exception e)
            {
                return false;
            }
            return true;
        }

        public Boolean memcpy(ref Byte[] buff, int buffIndex, Int64 data)
        {
            try
            {
                byte[] cb = BitConverter.GetBytes(data);
                for (int i = 0; i < cb.Length; i++)
                    buff[buffIndex + i] = cb[i];
            }
            catch (Exception e)
            {
                return false;
            }
            return true;
        }

        public Boolean memcpy(ref Int64 data, Byte[] buff, int buffIndex)
        {
            try
            {
                data = BitConverter.ToInt64(buff, buffIndex);
            }
            catch (Exception e)
            {
                return false;
            }
            return true;
        }
        public Boolean memcpy(ref Byte[] buff, int buffIndex, UInt64 data)
        {
            try
            {
                byte[] cb = BitConverter.GetBytes(data);
                for (int i = 0; i < cb.Length; i++)
                    buff[buffIndex + i] = cb[i];
            }
            catch (Exception e)
            {
                return false;
            }
            return true;
        }
        public Boolean memcpy(ref UInt64 data, Byte[] buff, int buffIndex)
        {
            try
            {
                data = BitConverter.ToUInt64(buff, buffIndex);
            }
            catch (Exception e)
            {
                return false;
            }
            return true;
        }
    }
}